
// 卡牌颜色
enum CardColor {
    red = 0,
    green = 1,
    blue = 2,
    yellow = 3,
    special = 4     //特殊牌
}

// 卡牌类型
enum CardType {
    number = 0,
    ban = 1,
    turn = 2,
    add_2 = 3,
    changeColor = 4,
    add_4 = 5
}
//卡牌状态
enum CardStatus {
    CLOSE = 0,
    OPEN = 1
}

// 卡牌类
export class Card {
    public color: CardColor = CardColor.red
    public type: CardType = CardType.number
    public index: number = 0
    // public UICard: UICard = null
    public status: CardStatus = CardStatus.CLOSE
    // public spriteFrame: cc.SpriteFrame = null
    // public location: CardLocation = CardLocation.sendCardArea
    constructor(color: CardColor, type: CardType, index: number, status: CardStatus = CardStatus.CLOSE) {
        this.color = color
        this.type = type
        this.index = index
        this.status = status
        // this.spriteFrame = spriteFrame
    }

    // public bindView(UICard: UICard) {
    //     this.UICard = UICard
    // }

    // public flopCard() {
    //     this.status = CardStatus.OPEN
    //     this.UICard.flopCard(this.spriteFrame)
    // }

    public level() {
        let level = 10;
        if (this.color == CardColor.blue && this.index == 3) return level;

        level--;
        if (this.color == CardColor.special && this.index == 16) return level;

        level--;
        if (this.color == CardColor.special && this.index == 17) return level;

        level--;
        if (this.index == 7) return level;

        level--;
        if (this.index == 2) return level;

        level--;
        return level;
    }

    public showGreater(card: Card, currentColor: CardColor = CardColor.special): boolean {
        if (this.index == card.index && this.color == card.color) return true;

        let l1 = this.level()
        let l2 = card.level()
        // console.log("l1", l1, "l2", l2)
        if (l1 != l2) {
            return l1 - l2 >= 0 ? true : false;
        }

        //等级相同
        if (this.color == card.color) {//同样花色
            return this.index - card.index >= 0 ? true : false;
        }

        if (this.color === currentColor) return true;
        if (card.color === currentColor) return false;

        return this.color - card.color >= 0 ? true : false;
    }
}

//玩牌区类
export class playerCardArea {
    public cards: Card[] = []

    public addCards(card: Card, color = CardColor.yellow) {
        let fIndex = this.cards.findIndex(element => {
            return card.showGreater(element, color)
        });
        if (fIndex === -1) fIndex = this.cards.length
        this.cards.splice(fIndex, 0, card)
    }

    //查找对应颜色卡牌
    public findCardByColor(color: CardColor, index: number): Card {
        let card: Card = null
        // 是否有相同的花色
        let hasCard = this.cards.some((item) => {
            if (item.color === color) {
                card = item
                return true
            }
            return false
        })
        if (!hasCard) {
            // 是否有相同的点数
            let hasSameIndex = this.cards.some((item) => {
                if (item.index === index && item.color !== CardColor.special) {
                    card = item
                    return true
                }
                return false
            })
            //是否有特殊牌
            if (!hasSameIndex) {
                this.cards.some((item) => {
                    if (item.color === CardColor.special) {
                        card = item
                        return true
                    }
                    return false
                })
            }

        }
        return card
    }

    // 牌区没牌？
    public isEmptyCard() {
        return this.cards.length === 0
    }

    // 牌区只剩一张？
    public isRemainOneCard() {
        return this.cards.length === 1
    }

    // 这张牌能不能出？
    public isShowCard(card: Card, color: CardColor, index: number): boolean {
        if (card.color === color) { return true }
        else if (card.index === index && card.color !== CardColor.special) { return true }
        else if (card.color === CardColor.special) { return true }
        else { return false }
    }

    // 删除指定牌
    public removeCard(card: Card) {
        this.cards.some((item, index) => {
            if (item == card) {
                this.cards.splice(index, 1)
                return true
            }
        })
    }
}

let cardArea = new playerCardArea()
for (let index = 2; index < 15; index++) {
    for (let index1 = 0; index1 < 4; index1++) {
        let card = new Card(index1, CardType.number, index)
        cardArea.addCards(card)
    }

}

// cardArea.addCards(new Card(CardColor.special, CardType.number, 16))
// cardArea.addCards(new Card(CardColor.special, CardType.number, 17))

// cardArea.addCards(new Card(1, CardType.number, 2))
// cardArea.addCards(new Card(2, CardType.number, 3))
// cardArea.addCards(new Card(1, CardType.number, 3))


// let card = new Card(CardColor.red, CardType.number, 1)
// let card1 = new Card(CardColor.red, CardType.number, 3)
// let card2 = new Card(CardColor.red, CardType.number, 5)



// cardArea.addCards(card)
// console.log(cardArea.cards)
// cardArea.addCards(card1)
// console.log(cardArea.cards)
// cardArea.addCards(card2)

// console.log(cardArea.cards)
const CardNodeMap = new Map<string, number>([
    [`${CardColor.blue}.3`, 32],
    [`${CardColor.special}.0`, 6],
    [`${CardColor.special}.1`, 5],

    [`${CardColor.yellow}.13`, 13],
]);

let index = CardNodeMap.get(`${CardColor.blue}.4`)
console.log(index)